<?php 
$css = ['/assets/jedate/skin/jedate.css'];
$this->load->view('admin/layout/head', array('title'=>"类别管理",'nav'=>'product','css'=>$css), FALSE);
switch ($this->session->admin_language) {
	case 'chinese':
		$prefix = '';
		break;
	case 'english':
		$prefix = 'en_';
		break;
	default:
		$prefix = '';
		break;
}
?>

<div id="container">
	<div class="toolbar">
		<form action="#" class="form-inline" id="search-form" method="get" >
			<div class="form-group">
				<input type="text" name="code" class="form-control" placeholder="请扫描商品条形码" style="width: 500px;">
			</div>
			<div class="form-group">
				<a href="javascript:;" class="btn btn-info search-btn">确定添加</a>
			</div>
		</form>
	</div>
	<form id="purchase-form" >
		<div class="mt-20">
			
			<table id="spec-table" class="table table-bordered table-striped table-hover scroll-table">
				<thead>
					<tr>
						<th>商品名称</th>
						<th>规格</th>
						<th>原价</th>
						<th>售价</th>
						<th>税费</th>
						<th>进货价</th>
						<th>数量</th>
						<th>过期时间</th>
						<th>备注</th>
						<th>删除</th>
					</tr>
				</thead>
				<tbody>
				</tbody>
			</table>
		</div>
		<div class="mt-20">
	      <a href="javascript:;" class="btn btn-primary save"><?= $this->lang->line('save') ?></a>
	      <a onclick="window.history.go(-1)" class="btn btn-default cancel"><?= $this->lang->line('cancel') ?></a>
	    </div>
	</form>
	
</div>

<?php 
$js = ['/assets/jedate/jedate.min.js'];
$this->load->view('admin/layout/footer',['js'=>$js]);
?>
<script>
$(function () {

	$('.search-btn').on('click',function(){
		if(!$('input[name=code]').val()){
			Toast.error('条形码内容不能为空');
			return;
		}
		var url = '<?= site_url('admin/purchaseRecord/search') ?>?code='+$('input[name=code]').val();

		$.getJSON(url, function (data){
			if(data.error){
				Toast.error('未找到商品');
				return;
			}
			if($("#tr-"+data.id).length>0){
				Toast.error('此商品已经添加过了');
				return;
			}
			var html = '<tr id="tr-'+data.id+'">\
							<td><input type="hidden" name="item[product_id][]" value="'+data['product_id']+'">'+data['p_<?= $prefix ?>title']+'</td>\
							<td><input type="hidden" name="item[item_id][]" value="'+data['id']+'">'+data['<?= $prefix ?>title']+'</td>\
							<td>'+data['original_price']+'</td>\
							<td>'+data['price']+'</td>\
							<td>'+data['tax']+'</td>\
							<td><input type="text" name="item[price][]" class="require" style="width:60px"></td>\
							<td><input type="text" name="item[number][]" class="require" style="width:60px"></td>\
							<td><input type="text" name="item[expire][]" class="require expire-'+data['id']+'" style="width:100px"></td>\
							<td><input type="text" name="item[remark][]" style="width:200px"></td>\
							<td><a href="javascript:;">删除</a></td>\
						</tr>';
			$('#spec-table tbody').append(html);
			datepicker('.expire-'+data.id,'<?php echo $this->session->admin_language ?>');
		});
	})

	$('body').on('click','#spec-table tbody a',function(){
		$(this).parents('tr').remove();
	})

	//保存
	$('.save').on('click', function () {
		var form = $('#purchase-form');
		
		var inputs = form.find('input');
		if (inputs.length < 1) {
			Toast.error('请至少填写一个进货规格');
			return false;
		}
		var requires = $('.require');
		for(var k in requires) {
			if (requires[k].value == '') {
				Toast.error('进货价，数量，过期日期必须填写');
				return false;
			}
		}
		
		$.post('/admin/purchaseRecord/create', form.serialize(), function (data) {
			Toast.success('创建成功，相应的商品库存已增加', function () {
				window.location.href = '/admin/purchaseRecord/index';
			});
		});
	});
});
</script>